"use strict";
cc._RF.push(module, '58755ljvghI4KXkaiwI6w7B', 'Game');
// scripts/Game.ts

Object.defineProperty(exports, "__esModule", { value: true });
var MetaballMgr_1 = require("./MetaballMgr");
var Model_1 = require("./Model");
var Config_1 = require("./Config");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var Game = /** @class */ (function (_super) {
    __extends(Game, _super);
    function Game() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.timeArray = [2.7, 3, 2.8, 4.3, 4.5, 5, 4.8, 7, 2.3, 3.4, 0.5, 2.2, 1.2, 1.6, 1.5, 1, 3.5, 1];
        _this.Ball = null;
        _this.Cup = null;
        _this.Wall = null;
        _this.wallContainer = null;
        _this.content = null;
        _this.topNode = null;
        _this.wrold = null;
        _this.drawMgr = null;
        _this.faceNode = null;
        _this.TopUI = null;
        _this.BottomUI = null;
        _this.timeLabel = null;
        _this.createDelay = 0.05;
        _this.createDelayTime = 1;
        _this.canCreate = false;
        _this.touchX = 0;
        _this.touchY = 0;
        _this.countDownNode = null;
        _this.countDownTime = 0;
        _this.linePosY = -250;
        _this.startCheckY = -212;
        _this.lock = false;
        _this.faceChange = false;
        _this.waterClip = null;
        _this.waterSoundDelay = 0.2;
        _this.waterSoundDelayCount = 10;
        _this.waters = [];
        _this.scTime = 0;
        _this.isJiLiBoFang = false;
        _this.callback = function () {
            this.scTime = this.scTime + 0.1;
            if (this.timeArray[Model_1.default.game.selectedLevel - 1] < this.scTime) {
                this.timeLabel.getComponent(cc.Label).string = '停止';
                // this.lock = true
                // this.canCreate = false;
            }
            else {
                this.timeLabel.getComponent(cc.Label).string = this.scTime.toFixed(1) + '';
            }
        };
        _this.totalBall = 0;
        return _this;
    }
    ;
    Game.prototype.onLoad = function () {
        this.timeLabel.active = false;
        Config_1.default.init();
        //开启物理效果
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = new cc.Vec2(0, -500);
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_jointBit | cc.PhysicsManager.DrawBits.e_shapeBit;
        // this.schedule(this.addBall,0.2);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        var top = cc.instantiate(this.TopUI);
        this.node.addChild(top);
        this.drawMgr.getComponent(MetaballMgr_1.default).waters = this.waters;
        this.node.getChildByName('gameTip').active = Model_1.default.game.selectedLevel == 1;
        this.createWorld();
    };
    Game.prototype.createWorld = function () {
        //cup
        var level = 1;
        var data = Config_1.default.preciseLevels[Model_1.default.game.selectedLevel];
        var cup = cc.instantiate(this.Cup);
        this.content.addChild(cup, 0);
        cup.x = data[1];
        cup.y = data[2];
        var cupData = Config_1.default.cupDatas[Model_1.default.game.selectedLevel];
        for (var i = 0; i < cupData.polygonColliderList.length; i++) {
            cup.addComponent(cc.PhysicsPolygonCollider);
        }
        var self = this;
        cc.loader.loadRes("textures/cup/cup" + data[0], cc.SpriteFrame, function (err, spriteFrame) {
            cup.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });
        //添加改变后，要用apply
        var allCollider = cup.getComponents(cc.PhysicsPolygonCollider);
        for (i = 0; i < allCollider.length; i++) {
            var tempC = allCollider[i];
            tempC.points = cupData.polygonColliderList[i];
            tempC.apply();
        }
        this.linePosY = cupData.linePosY + cup.y;
        this.curCup = cup;
        //face
        this.faceNode.active = true;
        this.faceNode.x = cup.x + cupData.faceOffsetX;
        this.faceNode.y = cup.y + cupData.faceoffsetY;
        this.faceNode.scale = cupData.faceScale;
    };
    Game.prototype.createWall = function (x, y, w, h, angle) {
        var wall = cc.instantiate(this.Wall);
        wall.width = w;
        wall.height = h;
        wall.x = x;
        wall.y = y;
        wall.angle = -angle;
        wall.getComponent(cc.PhysicsBoxCollider).size = cc.size(w, h);
        this.wallContainer.addChild(wall);
    };
    Game.prototype.onTouchStart = function (e) {
        if (this.isJiLiBoFang) {
            this.schedule(this.callback, 0.1);
        }
        if (this.lock)
            return;
        var pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        this.touchX = pos.x;
        this.touchY = pos.y;
        //this.scheduleOnce(this.startCreateBall,0.1)
        this.canCreate = true;
    };
    Game.prototype.onTouchEnd = function (e) {
        if (this.isJiLiBoFang) {
            this.scTime = 0;
            this.unschedule(this.callback);
        }
        // else{
        this.lock = true;
        this.canCreate = false;
        // }
    };
    Game.prototype.startCreateBall = function () {
        this.canCreate = true;
    };
    Game.prototype.start = function () {
    };
    Game.prototype.addBall = function (x, y) {
        var ball = cc.instantiate(this.Ball);
        ball.x = x;
        ball.y = y;
        ball.getComponent(cc.PhysicsCircleCollider).restitution = 0.2;
        ball.getComponent(cc.PhysicsCircleCollider).apply();
        this.wrold.addChild(ball);
        this.waters.push(ball);
    };
    Game.prototype.update = function (dt) {
        if (this.canCreate) {
            this.createDelayTime += dt;
            if (this.createDelayTime >= this.createDelay) {
                this.createDelayTime = 0;
                // if (this.totalBall < 50) {
                this.totalBall++;
                this.addBall(-135 + 20 - Math.random() * 40, 290);
                this.waterSoundDelayCount += dt;
                if (this.waterSoundDelayCount > this.waterSoundDelay) {
                    this.waterSoundDelayCount = 0;
                    cc.audioEngine.playEffect(this.waterClip, false);
                }
                // }
            }
        }
        this.drawMgr.getComponent(MetaballMgr_1.default).draw();
        if (this.lock && this.countDownNode.active == false) {
            this.checkGame();
        }
    };
    Game.prototype.checkGame = function () {
        if (this.checkAllDown()) {
            console.log('检测');
            this.countDownTime = 3;
            this.countDownNode.active = true;
            this.schedule(this.startCountDown, 1);
        }
        if (this.faceChange == false) {
            for (var _i = 0, _a = this.waters; _i < _a.length; _i++) {
                var temp = _a[_i];
                if (temp.y <= this.linePosY) {
                    this.faceChange = true;
                    this.showFace(1);
                    break;
                }
            }
        }
    };
    Game.prototype.startCountDown = function () {
        this.countDownNode.getChildByName('p0').active = false;
        this.countDownNode.getChildByName('p1').active = false;
        this.countDownNode.getChildByName('p2').active = false;
        this.countDownNode.getChildByName('p3').active = false;
        this.countDownTime--;
        this.countDownNode.getChildByName('p' + this.countDownTime).active = true;
        if (this.countDownTime <= 0) {
            this.countDownNode.getChildByName('p0').active = false;
            this.unschedule(this.startCountDown);
            this.schedule(this.checkBallSleep, 0.016);
        }
    };
    //判断所有的是否已经下落，开始检测
    Game.prototype.checkAllDown = function () {
        var bol = true;
        for (var _i = 0, _a = this.waters; _i < _a.length; _i++) {
            var temp = _a[_i];
            if (temp.y > this.startCheckY) {
                bol = false;
                break;
            }
        }
        return bol;
    };
    Game.prototype.checkBallSleep = function () {
        var bol = true;
        var goalCount = 0;
        for (var _i = 0, _a = this.waters; _i < _a.length; _i++) {
            var temp = _a[_i];
            //暂时不检测休眠
            // if (temp.getComponent(cc.RigidBody).awake == true) {
            //     bol = false;
            //     break;
            // }
            if (temp.y >= this.linePosY) {
                goalCount++;
            }
        }
        if (bol) {
            console.log('全部休眠');
            this.unschedule(this.checkBallSleep);
            if (goalCount >= 2) {
                console.log('游戏胜利');
                Model_1.default.game.win = true;
                Model_1.default.game.preciseLevel++;
                this.showFace(2);
                this.scheduleOnce(this.delayShowEnd.bind(this), 1);
            }
            else {
                Model_1.default.game.win = false;
                console.log('游戏失败');
                this.showFace(0);
                this.scheduleOnce(this.delayShowEnd.bind(this), 1);
            }
        }
    };
    Game.prototype.delayShowEnd = function () {
        cc.director.loadScene('gameEnd');
    };
    Game.prototype.showFace = function (type) {
        this.faceNode.getChildByName('facenormal').active = false;
        this.faceNode.getChildByName('facewin').active = false;
        this.faceNode.getChildByName('facelose').active = false;
        if (type == 2) {
            this.faceNode.getChildByName('facewin').active = true;
        }
        else if (type == 1) {
            this.faceNode.getChildByName('facelose').active = true;
        }
        else if (type == 0) {
            this.faceNode.getChildByName('facenormal').active = true;
        }
    };
    Game.prototype.retryClick = function () {
        cc.director.loadScene('game');
    };
    Game.prototype.onBackClick = function () {
        cc.director.loadScene('menu');
    };
    Game.prototype.onBtn = function () {
        console.log("@@@@@@@@@@@@播放激励视频");
        this.isJiLiBoFang = true;
        this.timeLabel.active = true;
    };
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "Ball", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "Cup", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "Wall", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "wallContainer", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "content", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "topNode", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "wrold", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "drawMgr", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "faceNode", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "TopUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "BottomUI", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "timeLabel", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "countDownNode", void 0);
    __decorate([
        property({ type: cc.AudioClip })
    ], Game.prototype, "waterClip", void 0);
    Game = __decorate([
        ccclass
    ], Game);
    return Game;
}(cc.Component));
exports.default = Game;
//1-2  = 3
//3   = 3.5
//4 5     = 4
//6    =4.5

cc._RF.pop();